﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using System.Text;
using System.Configuration;
using Erros = Caduceu.Estudos.Erros;

namespace Caduceu.Estudos.DAL
{
    public class DbFactory
    {
        private const string ERR_DB_INDEFINIDO = "ERR_0001";
        private const string ERR_ZERO_CONNSTR = "ERR_0002";
        
        protected static DbProviderFactory factory;
        protected static string defaultConnName = null;
        protected static Dictionary<string, string> dictConnections;
        
        // static constructor
        static DbFactory()
        {
            string databaseImpl;
            
            if (string.IsNullOrEmpty(ConfigurationManager.AppSettings.Get("databaseImpl"))) {
                throw new Exception(Erros.Messages.GetMessage2(ERR_DB_INDEFINIDO));
            }

            databaseImpl = ConfigurationManager.AppSettings["databaseImpl"];

            // Verificando as connection strings definidas. Se não houver nenhuma, 
            // será lançada uma exceção

            dictConnections = new Dictionary<string, string>();

            foreach (ConnectionStringSettings connectionString in ConfigurationManager.ConnectionStrings)
            {                
                if (defaultConnName == null) defaultConnName = connectionString.Name;
                dictConnections[connectionString.Name] = connectionString.ConnectionString;
            }
                
            
            if (dictConnections.Count == 0)
                throw new Exception(Erros.Messages.GetMessage2(ERR_ZERO_CONNSTR));

            factory = DbProviderFactories.GetFactory(databaseImpl);
        }

        public static DbConnection CreateConnection()
        {
            return factory.CreateConnection();
        }

        public static DbConnection CreateConfiguredConnection()
        {            
            return CreateConfiguredConnection(defaultConnName);
        }
        
        public static DbConnection CreateConfiguredConnection(string connStrName)
        {
            string connStr = dictConnections[connStrName];
            DbConnection conn = factory.CreateConnection();
            conn.ConnectionString = connStr;
            return conn;
        }

        public static DbCommand CreateCommand()
        {
            return factory.CreateCommand();
        }

        public static DbDataAdapter CreateDataAdapter()
        {
            return factory.CreateDataAdapter();
        }

        public static DbParameter CreateParameter()
        {
            return factory.CreateParameter();
        }

        public static DbParameter CreateParameter(string paramName, object paramValue)
        {
            DbParameter param = factory.CreateParameter();
            param.ParameterName = paramName;
            param.Value = paramValue;
            return param;
        }
    }
}
